<?php

/**
 * This is the model class for table "{{notify}}".
 *
 * The followings are the available columns in table '{{notify}}':
 * @property string $uid
 * @property string $type
 * @property string $num
 */
class NotifyData extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @return NotifyDao the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{notify}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('uid, type', 'required'),
            array('uid, type, num', 'length', 'max'=>10),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('uid, type, num', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'uid' => 'Uid',
            'type' => 'Type',
            'num' => 'Num',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('uid',$this->uid,true);
        $criteria->compare('type',$this->type,true);
        $criteria->compare('num',$this->num,true);

        return new CActiveDataProvider(get_class($this), array(
            'criteria'=>$criteria,
        ));
    }
    
    public static function addNotify($uid, $type)
    {
        $arr_cond = array('uid' => $uid, 'type' => $type);
        $ret = self::model()->findData(null, $arr_cond);
        if (empty($ret))
        {
            // 该用户的信息还不存在，插一条新记录
            $notify = new NotifyData();
            $notify->uid = $uid;
            $notify->type = $type;
            $notify->num = 1;
            $notify->insert();
        } else
        {
            // 该用户的信息已存在，更新
            $notify = $ret[0];
            $notify->num = intval($notify->num) + 1;
            $notify->setIsNewRecord(false);
            $notify->update(array('num'));
        }
    }
    
    public static function releaseNotify($uid, $type)
    {
        $arr_cond = array('uid' => $uid, 'type' => $type);
        $ret = self::model()->findData(null, $arr_cond);
        if (!empty($ret))
        {
            $notify = $ret[0];
            $notify->num = 0;
            $notify->setIsNewRecord(false);
            $notify->update(array('num'));
        }
    }
    
    public static function getNotify($uid)
    {
        $arr_cond = array('uid' => $uid);
        $ret = self::model()->findData(null, $arr_cond);
        return $ret;
    }
}
